package com.ruoyi.system.service.impl;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.config.oss.OssProperties;
import com.ruoyi.common.config.oss.OssTemplate;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.vo.InspectionPlanTaskToVO;
import com.ruoyi.system.domain.vo.PointProjectVO;
import com.ruoyi.system.entity.InspectionPoint;
import com.ruoyi.system.mapper.InspectionPlanTaskDetailsMapper;
import com.ruoyi.system.mapper.InspectionPointMapper;
import com.ruoyi.system.service.InspectionPlanTaskDetailsService;
import com.ruoyi.system.service.InspectionPointService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

/**
 * (InspectionPoint)表服务实现类
 * @since 2022-11-25 17:10:29
 */
@Service("inspectionPointService")
public class InspectionPointServiceImpl implements InspectionPointService {
    @Resource
    private InspectionPointMapper inspectionPointMapper;
    @Resource
    private InspectionPlanTaskDetailsMapper inspectionPlanTaskDetailsMapper;
    @Autowired
    private OssTemplate template;
    @Autowired
    private OssProperties ossProperties;
    @Resource
    private InspectionPlanTaskDetailsService inspectionPlanTaskDetailsService;

    /**
     * 通过ID查询单条数据
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public InspectionPoint queryById(Integer id) {
        return this.inspectionPointMapper.queryById(id);
    }

    /**
     * 分页查询
     * @param inspectionPoint 筛选条件
     * @return 查询结果
     */
    @Override
    public List<InspectionPoint> queryByPage(InspectionPoint inspectionPoint) {
        return this.inspectionPointMapper.queryAllByLimit(inspectionPoint);
    }

    /**
     * 新增数据
     * @param inspectionPoint 实例对象
     * @return 实例对象
     */
    @Override
    public InspectionPoint insert(InspectionPoint inspectionPoint) throws JsonProcessingException {
        this.inspectionPointMapper.insert(inspectionPoint);
        String id = inspectionPoint.getId().toString();

        JSONObject object = new JSONObject();
        object.put("id",id);
        object.put("type", Constants.IN);
        String json = JSONUtil.toJsonStr(object);

        File file = new File(id + ".jpg");
       // File generate = QrCodeUtil.generate(id, 300, 300,type, file);
        File generate = QrCodeUtil.generate(json, 300, 300, file);
        InputStream in = null;
        try {
            in = new FileInputStream(generate);
            template.putObject(ossProperties.getBucketName(),id + ".jpg",in);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(in);
            boolean delete = file.delete();
            if (delete) {
                System.out.println("删除临时二维码文件成功");
            } else {
                System.out.println("删除临时二维码文件失败");
            }
        }
        String objectURL = ossProperties.getAgency() + "/" + ossProperties.getBucketName() + "/" + id + ".jpg";
        inspectionPoint.setCodeImageFileUrl(HexUtil.encodeHexStr(objectURL));
        inspectionPointMapper.update(inspectionPoint);
        return inspectionPoint;
    }

    /**
     * 修改数据
     * @param inspectionPoint 实例对象
     * @return 实例对象
     */
    @Override
    public InspectionPoint update(InspectionPoint inspectionPoint) {
        Boolean at = false;
        // 查询点位排序数值是否有重复的
        if (0 !=inspectionPoint.getPointOrder()){
            InspectionPoint point = this.inspectionPointMapper.getUpRouteId(inspectionPoint.getRouteId(),inspectionPoint.getPointOrder());
            if (!ObjectUtils.isEmpty(point)){
                at = true;
            }
            if (at) {
                throw new ServiceException("当前排序编号有重复，请重新排序",500);
            }
        }
        this.inspectionPointMapper.update(inspectionPoint);
        return this.queryById(inspectionPoint.getId());
    }


    /**
     * 通过主键删除数据
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Integer id) {
        inspectionPlanTaskDetailsMapper.deleteByPointId(id);
        return this.inspectionPointMapper.deleteById(id) > 0;
    }


    @Override
    public PointProjectVO getPointAndProjectById(Integer id) {
        return inspectionPointMapper.getPointAndProjectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateStopCheck(InspectionPoint inspectionPoint) {
        Integer id = inspectionPoint.getId();
        Integer stopCheck = inspectionPoint.getStopCheck();
        if (stopCheck == 1) {
            inspectionPlanTaskDetailsService.updateStateByPointId(id);
        }
        inspectionPointMapper.update(inspectionPoint);
    }

    // 点位排序
    @Override
    public InspectionPoint getUpPoIntOrderId(Integer id, Integer pointOrder) {
        return inspectionPointMapper.getUpPoIntOrderId(id,pointOrder);
    }


    /**
     *  通过巡检点ID  获取巡检任务
     * @param id
     * @return
     */
    @Override
    public List<InspectionPlanTaskToVO> getByIdtOTaskInfo(Integer id) {
        return inspectionPlanTaskDetailsMapper.getByIdtOTaskInfo(id);
    }
}
